#### Setup ####
data <- readRDS("dataset_leip_dataverse.rds")

#### Prep data ####
## Set `pcteligible2013` to % value
data <- data %>% 
  mutate(pcteligible2013 = pcteligible2013 * 100)

## Create dataframe for the four graphs
df5 <- data %>%
  select(dregistration20142010,
         dregistration20162012,
         dturnout20142010,
         dturnout20162012,
         pcteligible2013,
         pctwhite,
         z) %>%
  mutate(z = if_else(z == 0, "Not Expanding", "Expanding")) %>%
  mutate(z_f = factor(z, levels = c("Not Expanding", "Expanding"))) %>%
  mutate(diverse = ifelse(pctwhite >= median(pctwhite, na.rm = TRUE), 
                          "More diverse", "Less diverse")) %>%
  mutate(diverse = factor(diverse, levels = c("More diverse", "Less diverse")))

#### V1 Plot data ####
df5 %>%
ggplot(aes_string(x = "pcteligible2013",
                  y = "dregistration20142010",
                  col = "diverse", shape = "diverse", type = "diverse"),
       na.rm = TRUE) +
  geom_point(alpha = 0.2) +
  facet_grid(. ~ z_f) +
  scale_x_continuous(limit = c(0, 60)) +
  scale_y_continuous(limit = c(-20, 10)) +
  labs(
    x = "Potential Eligibility",
    y = "Change in Participation",
    title = "Registration 2014 - 2010") +
  
  stat_smooth(method = "lm",
              #col = diverse,
              se = FALSE,
              size = 0.7)



#### V2 PLOT DATA ####
## Prep objects for loop
titles <- c("Registration 2014 - 2010",
            "Registration 2016 - 2012",
            "Turnout 2014 - 2010",
            "Turnout 2016 - 2012")
y_names <- names(df5[, c(1:4)])

plot_list5 <- list()


## Loop
for(i in 1:4) {
  plot_list5[[i]] <- df5 %>%
    ggplot(aes_string(x = "pcteligible2013",
                      y = y_names[i],
                      col = "diverse", shape = "diverse", type = "diverse"),
           na.rm = TRUE) +
    geom_point(alpha = 0.1) +
    facet_grid(. ~ z_f) +
    scale_x_continuous(limit = c(0, 60)) +
    scale_y_continuous(limit = c(-20, 10)) +
    labs(
      x = "Potential Eligibility",
      y = "Change in Participation",
      title = titles[i]) +
    
    # Customize appearance
    theme_classic() +
    theme(axis.line = element_line(size = 0.25),
          strip.background = element_rect(colour = "white", fill = "white"),
          plot.title = element_text(hjust = 0.5)) +
    
    # Add regression line 
    stat_smooth(method = "lm",
                se = FALSE,
                size = 0.7)
  
}

fig5 <- ggarrange(plot_list5[[1]] +
            theme(axis.title.x = element_blank(),
                  axis.title.y = element_blank()), 
          plot_list5[[2]] +
            theme(axis.text.y = element_blank(),
                  axis.ticks.y = element_blank(),
                  axis.title.y = element_blank(),
                  axis.title.x = element_blank()),
          plot_list5[[3]]+
            theme(axis.title.x = element_blank(),
                  axis.title.y = element_blank()),
          plot_list5[[4]]+
            theme(axis.text.y = element_blank(),
                  axis.ticks.y = element_blank(),
                  axis.title.y = element_blank(),
                  axis.title.x = element_blank()), 
          nrow = 2, ncol = 2,
          common.legend = TRUE)

annotate_figure(fig5,
                bottom = text_grob("Potential Eligibility"),
                left = text_grob("Change in Participation", rot = 90))
